package org.example;

//import com.alibaba.fastjson.JSONObject;
//import com.apitest.util.dataprovider.FileDriverUtils;
import org.testng.IReporter;
import org.testng.ISuite;
import org.testng.ISuiteResult;
import org.testng.ITestNGMethod;
import org.testng.xml.XmlSuite;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class ReportListener implements IReporter {
	int totalTestCase = 0;
	double passRate = 0;
	int failNum = 0;
	int passNum = 0;
	int skipNum = 0;
	String projectName="自动化接口测试（davinci收银台）";
	String jobName = "test-api-auto-cashier";
	ITestNGMethod[] testNGMethods;
	String clientTokenPath = "CLIENT_TOKEN_PATH";
	
	@Override
	public void generateReport(List<XmlSuite> xmlSuites, List<ISuite> suites, String outputDirectory) {
		for (ISuite suite : suites) {
			Map<String, ISuiteResult> results = suite.getResults();
			projectName += results.keySet();
			for (ISuiteResult suiteResult : results.values()) {
				testNGMethods = suiteResult.getTestContext().getAllTestMethods();
				passNum += suiteResult.getTestContext().getPassedTests().getAllResults().size();
				failNum += suiteResult.getTestContext().getFailedTests().getAllResults().size();
				skipNum += suiteResult.getTestContext().getSkippedTests().getAllResults().size();
			}
		}
		totalTestCase = passNum + failNum + skipNum;
		passRate = new BigDecimal((float) passNum / totalTestCase * 100).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		//钉钉webhook
//		String url = "https://oapi.dingtalk.com/robot/send?access_token=2b5b93314530c0a3e30d8a0e8d2ac812894b6d1690bdfd45066730ae98124a66";
//		//String url = "https://oapi.dingtalk.com/robot/send?access_token=511471886b223799eb6ff71ae438bcc777546398774e4a24a029ee11822f5239";
//		//签名密钥
//		String secret = "SEC665ee8277a14740e58747384a1fd935efe8a8b1078280ffc3f2fd942927038a1";
//		//String secret = "SECa91719b0392a6298cb69f970e33f41a38a4a069a82ba8eac24bfe50b79dbc34e";
//		// 调用钉钉工具类的发消息方法
//		String isDingTalk = "";
//		try {
//			isDingTalk = FileDriverUtils.readFileContent("isDingTalk.txt",clientTokenPath);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		//1，发送钉钉通知
//		if (isDingTalk.equals("1")){
//			DingTalkRobot dingTalkRobot = new DingTalkRobot(secret, url);
//			try {
//				dingTalkRobot.sendLinkMsg(convertToLink());
//				//dingTalkRobot.sendLinkMsg(convertToLink1());
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
//	}
//
//	/**
//	 * 组装发送的消息
//	 */
//	private String convertToLink() {
//		JSONObject sendObj = new JSONObject();
//		JSONObject linkObj = new JSONObject();
//		linkObj.put("title", projectName);
//		linkObj.put("text", "用例数：" + totalTestCase + " 通过率：" + passRate+"%"+ "\n通过：" + passNum + "\t失败：" + failNum + "\t跳过：" + skipNum );
//		//linkObj.put("text","执行的测试用例："+testNGMethods);
//		linkObj.put("picUrl", "");
//		linkObj.put("messageUrl", "http://192.168.10.114:8080" + "/job/" + jobName + "/allure/");
//		sendObj.put("msgtype", "link");
//		sendObj.put("link", linkObj);
//
//		return sendObj.toJSONString();
//	}
//
//	private String convertToLink1(){
//		JSONObject sendObj1 = new JSONObject();
//		JSONObject linkObj1 = new JSONObject();
//		linkObj1.put("title", "2222");
//		linkObj1.put("text","测试2");
//		linkObj1.put("picUrl", "");
//		linkObj1.put("messageUrl", "http://192.168.10.114:8080" + "/job/" + jobName + "/allure/");
//		sendObj1.put("msgtype", "text");
//		sendObj1.put("text", linkObj1);
//		return sendObj1.toJSONString();
	}
	
}
